<?php

namespace App\Http\Controllers;

use App\Role;
use App\Service\PermissionService;
use App\Service\RoleService;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    protected $roleService;

    public function __construct(RoleService $roleService)
    {
        $this->roleService = $roleService;
    }

    /**
     * 返回角色列表
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     * create WenQing
     */
    public function index()
    {
        return $this->roleService->getAllRole();
    }

    /**
     * 显示单个角色信息
     * @param $id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     * create WenQing
     */
    public function show($id)
    {
        return $this->roleService->getRoleAndPerByRoleId($id);
    }

    public function edit(Request $request)
    {
        $role = Role::find($request->get('id'));
        if($request->get('perms'))
        {
            $role->perms()->sync($request->get('perms'));
        }
        if($request->get('name')) $role->name = $request->get('name');
        if($request->get('display_name')) $role->display_name = $request->get('display_name');
        if($request->get('description')) $role->description = $request->get('description');
        $role->save();

        return 'success';
    }
}
